package com.example.demo.Controller;



import com.example.demo.Entity.User;
import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")//允许所有同源访问，防止出现跨域问题
public class UserLoginControllerDemo {

    private final UserLoginServiceDemo userServiceDemo;
    private final JdbcTemplate jdbcTemplate;
    private final UserService userService;

    @Autowired
    public UserLoginControllerDemo(UserLoginServiceDemo userServiceDemo, JdbcTemplate jdbcTemplate,UserService userService) {
        this.userServiceDemo = userServiceDemo;
        this.jdbcTemplate = jdbcTemplate;
        this.userService = userService;
    }

    //注意前后端传输数据格式匹配问题
    //uniapp默认是前端以json格式传输，后端要接受需要这种格式(@RequestBody User user)


    //如果uniapp以表单格式传输(这是apipost的默认传输格式),即在method ：POST 后加上
    // (          header: {
    //            'Content-Type': 'application/x-www-form-urlencoded'
    //          },)，则后端要接受需要这种格式(@ModelAttribute User user)或者是不加注解(User user)


    @CrossOrigin(origins = "*")//允许所有同源访问，防止出现跨域问题
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody User u) {//@RequestParam("username") String username, @RequestParam("password") String password
        System.out.println("连接成功");

        System.out.println(u);
        String username = u.getUsername();
        String password = u.getPassword();

        System.out.println("username:" + username + "  password:" + password);
        Optional<User> user = userService.findByUsername(username);

        Map<String, Object> response = new HashMap<>();

        if (user.isPresent()) {
            if (user.get().getPassword().equals(password)) {//密码正确
                if(user.get().getStatus() == 0){
                    response.put("success", false);
                    response.put("message", "用户已被禁用");
                    return response;
                }
                response.put("success", true);
                response.put("message", "登录成功");
                response.put("isManager",userService.getUserByName(username).get().getRole().equals("admin"));
                response.put("userName", username); // You can add more user information if needed
                response.put("token", "exampleToken"); // Token generation logic should be implemented
                System.out.println("登录成功");
            } else {//密码错误
                response.put("success", false);
                response.put("message", "密码错误");
                System.out.println("密码错误");
            }
        } else {
            response.put("success", false);
            response.put("message", "用户名不存在");
            System.out.println("用户名不存在");
        }

        return response;
    }

    @PostMapping("/register")
    public Map<String, Object> register(User user) {
        System.out.println("连接成功");
        String username = user.getUsername();
        String password = user.getPassword();
        String email = user.getEmail();
        System.out.println("username:" + username + "  password:" + password);


        Map<String, Object> response = new HashMap<>();
        boolean success = userServiceDemo.registerUser(username,password);
        if (success) {
            response.put("success", true);
            response.put("message", "注册成功");
            response.put("userName", username); // You can add more user information if needed
            response.put("token", "exampleToken"); // Token generation logic should be implemented
            System.out.println("注册成功");
        } else {
            response.put("success", false);
            response.put("message", "注册失败：用户名重复");
            System.out.println("注册失败");
        }

        return response;
    }


}

